﻿using Echegoyen.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Data;

namespace Echegoyen
{

    public class MyContext : DbContext, Echegoyen.IContext
    {
        public IDbSet<Autor> Autor { get; set; }
        public IDbSet<Editorial> Editorial { get; set; }
        public IDbSet<Ejemplar> Ejemplar { get; set; }
        public IDbSet<Material> Material { get; set; }
        public IDbSet<Prestamo> Prestamo { get; set; }
        public IDbSet<Reserva> Reserva { get; set; }
        public IDbSet<Tema> Tema { get; set; }
        public IDbSet<TipoMaterial> TipoMaterial { get; set; }
        public IDbSet<Usuario> Usuario { get; set; }


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<Material>().
                HasMany(m => m.Tema).
                WithMany(t => t.Materiales).
                Map(
                    m =>
                    {
                        m.ToTable("Material_Tema");
                        m.MapLeftKey("MaterialId");
                        m.MapRightKey("TemaId");
                    });

            modelBuilder.Entity<Material>().
                HasMany(m => m.Autores).
                WithMany(a => a.Materiales).
                Map(
                    m =>
                    {
                        m.ToTable("Material_Autor");
                        m.MapLeftKey("MaterialId");
                        m.MapRightKey("AutorId");
                    });

        }


        public bool SetModified(object entity)
        {
            Entry(entity).State = EntityState.Modified;
            return true;
        }
    }
}